﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HHo_Negocio;
using HHo_Model;
using System.Text;
using System.IO;

public partial class Reportes_wfRepHHPrg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GestorTblParametros oGesPar = new GestorTblParametros();
            ddlArea.DataSource = oGesPar.ListaParametroPorGrupo(6);
            ddlArea.DataTextField = "ParametroValor";
            ddlArea.DataValueField = "ParametroID";
            ddlArea.DataBind();
        }
    }
    protected void imgBuscar_Click(object sender, ImageClickEventArgs e)
    {
        string uri_reportserver;
        uri_reportserver = ConfigurationManager.AppSettings["URLReportServer"].ToString();
        lblFrame.Text = "<iFrame width=\"1000px\" height=\"700px\" src=\"" + uri_reportserver + "?/HHo_ReportService/Report2" + "&rs:Command=Render&FECINFO=" + txtFecInf.Text + "&AREAID=" + ddlArea.SelectedValue + "&rc:Parameters=False&rc:ToolBar=True\"  />";
    }
    protected void imgToExcel_Click(object sender, ImageClickEventArgs e)
    {
        Label lbl = lblToExcel;
        //lbl.ID = "lblDatos";
        lbl.Text = HTMLTableDatos();


        string atachado = "attachment; filename=RepTrab.xls";
        Response.ClearContent();
        Response.Clear();
        Response.AddHeader("content-disposition", atachado);

        //Se supone debe abrir sin mostrar la ventanita de guardar
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);

        //Response.ContentType = "application/vnd.ms-excel";
        Response.ContentType = "application/ms-excel";
        Response.Charset = "";
        this.EnableViewState = false;

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        HtmlForm frm = new HtmlForm();
        lbl.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lbl);
        //sw.Write(PrepareHTMLEtiqExcel());
        //gvConsol.Parent.Controls.Add(frm);
        //frm.Controls.Add(gvConsol);

        frm.RenderControl(htw);
        //hidDataFiltro.RenderControl(htw);
        //gvResult.RenderControl(htw);        
        Response.Write(sw.ToString());
        Response.End();
    }

    private string HTMLTableDatos()
    {
        GestorReportes oGesRep;
        E_RepTrabajos eRepTrabajos;
        StringBuilder strHTML = new StringBuilder();
        StringBuilder strHeader = new StringBuilder();
        StringBuilder strDataTL = new StringBuilder();
        StringBuilder strDataLleg = new StringBuilder();
        StringBuilder strDataVloLleg = new StringBuilder();
        StringBuilder strDataSal = new StringBuilder();
        StringBuilder strDataVloSal = new StringBuilder();
        StringBuilder strDataTEM = new StringBuilder();
        StringBuilder strDataTotEst = new StringBuilder();
        StringBuilder strDataProg = new StringBuilder();
        StringBuilder strDataPersonal = new StringBuilder();
        StringBuilder strTrabajos = new StringBuilder();
        StringBuilder strPersonas = new StringBuilder();

        //string strHTML;
        oGesRep = new GestorReportes();
        //gvTrab.DataSource = oGesRep.CargaDatosRepTrabajos(Convert.ToDateTime(txtFecInf.Text), Int32.Parse(ddlArea.SelectedValue));
        //gvTrab.DataBind();

        eRepTrabajos = new E_RepTrabajos();
        eRepTrabajos.Id = -99; //Comodin para no valido;

        strHTML.Append("<table border='1' cellspacing='0' cellpadding='0'><thead><tr><th bgcolor='#CCCCCC'>AVION</th>");
        strDataTL.Append("<tbody><tr><td>TLeader</td>");
        strDataLleg.Append("<tr><td>Llegada:</td>");
        strDataVloLleg.Append("<tr><td>Vuelo</td>");
        strDataSal.Append("<tr><td>Salida:</td>");
        strDataVloSal.Append("<tr><td>Vuelo:</td>");
        strDataTEM.Append("<tr><td>TEM:</td>");
        strDataTotEst.Append("<tr><td>TOTAL ESTIMADO</td>");
        strDataProg.Append("<tr><td>PROGRAMADO</td>");
        strDataPersonal.Append("<tr><td>PERSONAL</td>");

        foreach (E_RepTrabajos eRep in oGesRep.CargaDatosRepTrabajos(Convert.ToDateTime(txtFecInf.Text), Int32.Parse(ddlArea.SelectedValue)))
        {
            //Cabecera asociada a nuevo ID
            if (eRepTrabajos.Id != eRep.Id)
            {
                eRepTrabajos.Id = eRep.Id;

                strHeader.Append("<th bgcolor='#CCCCCC'>" + eRep.MatAvion + "</th>");
                if (eRep.TleaderId != 0)
                {
                    strDataTL.Append("<td>" + eRep.TleaderId + "</td>");
                }
                else
                {
                    strDataTL.Append("<td>&nbsp;</td>");
                }

                if (eRep.StrNumVloLlegada != null)
                {
                    strDataLleg.Append("<td>" + eRep.StrNumVloLlegada + "</td>");
                }
                else
                {
                    strDataLleg.Append("<td>&nbsp;</td>");
                }

                if (eRep.DtHraLlegada != null)
                {
                    strDataVloLleg.Append("<td>" + eRep.DtHraLlegada.ToShortTimeString() + "</td>");
                }
                //else
                //{
                //    strDataVloLleg.Append("<td>&nbsp;</td>");
                //}

                if (eRep.StrNumVloSalida != null)
                {
                    strDataSal.Append("<td>" + eRep.StrNumVloSalida + "</td>");
                }
                else
                {
                    strDataSal.Append("<td>&nbsp;</td>");
                }

                if (eRep.DtHraSalida != null)
                {
                    strDataVloSal.Append("<td>" + eRep.DtHraSalida.ToShortTimeString() + "</td>");
                }
                //else
                //{
                //    strDataVloSal.Append("<td>&nbsp;</td>");
                //}

                if (eRep.DtHraTEM != null)
                {
                    strDataTEM.Append("<td>" + eRep.DtHraTEM.ToShortTimeString() + "</td>");
                }
                //else
                //{
                //    strDataTEM.Append("<td>&nbsp;</td>");
                //}

                if (eRep.TotEstimado != 0)
                {
                    strDataTotEst.Append("<td>" + eRep.TotEstimado.ToString() + "</td>");
                }
                else
                {
                    strDataTotEst.Append("<td>&nbsp;</td>");
                }

                if (strTrabajos.Length > 6)
                {
                    strTrabajos.Append("</table>");
                    strDataProg.Append("<td>" + strTrabajos.ToString() + "</td>");
                    strTrabajos.Remove(0, strTrabajos.Length);
                }

                if (strPersonas.Length > 6)
                {

                    strPersonas.Append("</table>");
                    strDataPersonal.Append("<td>" + strPersonas.ToString() + "</td>");
                    strPersonas.Remove(0, strPersonas.Length);
                }

                strTrabajos.Append("<table border='1' cellspacing='0' cellpadding='0'><tr><td bgcolor='#CCCCCC'>Trabajo</td><td bgcolor='#CCCCCC'>Nota</td></tr>");
                strPersonas.Append("<table border='1' cellspacing='0' cellpadding='0'><tr><td bgcolor='#CCCCCC'>Personal</td></tr>");

            }
            if (eRep.StrDescTrab != null)
            {
                strTrabajos.Append("<tr><td>" + eRep.StrDescTrab + "</td>");
            }
            else
            {
                strTrabajos.Append("<tr><td>&nbsp;</td>");
            }

            if (eRep.Nota != null)
            {
                strTrabajos.Append("<td>" + eRep.Nota + "</td></tr>");
            }
            else
            {
                strTrabajos.Append("<td>&nbsp;</td></tr>");
            }

            if (eRep.MecApMat != null)
            {
                strPersonas.Append("<tr><td>" + eRep.MecApMat + "</td></tr>");
            }
            else
            {
                strPersonas.Append("<tr><td>&nbsp;</td></tr>");
            }
        }

        if (strTrabajos.Length > 6)
        {
            strTrabajos.Append("</table>");
            strDataProg.Append("<td>" + strTrabajos.ToString() + "</td>");
            strTrabajos.Remove(0, strTrabajos.Length);
        }

        if (strPersonas.Length > 6)
        {

            strPersonas.Append("</table>");
            strDataPersonal.Append("<td>" + strPersonas.ToString() + "</td>");
            strPersonas.Remove(0, strPersonas.Length);
        }

        strHTML.Append(strHeader.ToString());
        strHTML.Append("</thead>");
        strHTML.Append(strDataTL.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataLleg.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataVloLleg.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataSal.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataVloSal.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataTEM.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataTotEst.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataProg.ToString());
        strHTML.Append("</tr>");
        strHTML.Append(strDataPersonal.ToString());
        strHTML.Append("</tr></tbody></table>");
        //lblToExcel.Text = strHTML.ToString();

        return strHTML.ToString();
    }
}
